package yl.hs.bmipfull.controllers.host;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.host.ProductionLineRawConfigEntity;
import yl.hs.bmipfull.service.host.IProductionLineCodeConfigService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作用:  生产线主机字段设置控制层<br/>
 * 创建时间: 2021年06月28日 14时34分32秒 <br/>
 *
 * @author 谢东
 */
@Api(value = "生产线主机字段设置Web接口", tags = "主机程序——生产线主机字段设置操作")
@RequestMapping(value = "/api/productionLineCodeConfig")
@RestController
public class ProductionLineCodeConfigController extends ControllerBase {

    @Autowired
    IProductionLineCodeConfigService service;

    @ApiOperation(value = "查询生产线主机字段设置", notes = "查询生产线主机字段设置")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询生产线主机字段设置", notes = "分页查询生产线主机字段设置")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询生产线主机字段设置", notes = "根据生产线编号查询生产线主机字段设置")
    @RequestMapping(value = "/{productionLineSN}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String productionLineSN) {
        return success("查询成功", service.findByLineSn(productionLineSN));
    }


    @ApiOperation(value = "保存生产线主机字段设置", notes = "查询/更新生产线主机字段设置信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveLineConfig(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            Map<String, Object> codeConfigModel = (Map<String, Object>) submitModel.getData().get("CodeConfigModel");
            Map<String, Object> sqlConfigModel = (Map<String, Object>) submitModel.getData().get("SqlConfigModel");
            List<Map<String, Object>> rawConfigModelList = (List<Map<String, Object>>) submitModel.getData().get("RawConfigList");

            List<ProductionLineRawConfigEntity> rawConfigList = new ArrayList<>();
            for (var item : rawConfigModelList) {
                ProductionLineRawConfigEntity entity = new ProductionLineRawConfigEntity();
                Tl.getChangeLog(entity, item);
                entity.setPlSn(submitModel.getSn());
                rawConfigList.add(entity);
            }
            result = service.saveProductionLineCodeConfig(codeConfigModel, sqlConfigModel, rawConfigList);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除生产线主机字段设置信息", notes = "根据生产线主机字段设置编号删除系生产线主机字段设置信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "生产线主机字段设置编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
}